package com.exam.action;

import java.sql.SQLException;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.exam.dao.AdminDao;
import com.exam.dao.StudentDao;
import com.exam.util.Tools;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements RequestAware,SessionAware{
	
	private String username;  	
	private String password; 	
	private String myId; 		
	
	private String message;
	private Map<String, Object> application;  
	public String checkLogin(){
		
		try {
			if(myId.equals("0")){
				StudentDao student = new StudentDao();
				if(student.checkStudentLogin(username, password)){
					session.put("student", password);
					session.put("username", username);
					application = ActionContext.getContext().getApplication();
					if(application.get("countStudent")==null){
						application.put("countStudent", 1);
					}else{
						int m = (Integer) application.get("countStudent")+1;
						application.put("countStudent", m);
					}
					return "student";
				}else{
					message = "登录失败，请检查用户名和密码!";
					return "error";
				}
			}else if(myId.equals("1")){
				AdminDao  admin = new AdminDao();
				System.out.println(Tools.sha1(password));
					if(admin.checkAdminLogin(username, Tools.sha1(password))){
						session.put("admin", username);
						return "admin";
					}else{
						message = "登录失败，请检查用户名和密码!";
						return "error";
					}
			}
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}


	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getMyId() {
		return myId;
	}
	public void setMyId(String myId) {
		this.myId = myId;
	}

	Map<String, Object> request = null;
	@Override
	public void setRequest(Map<String, Object> req) {
		this.request = req;
	}
	Map<String, Object> session = null;
	@Override
	public void setSession(Map<String, Object> sess) {
		this.session = sess;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}
